package com.appleyk.security.springmvc.config;

import com.appleyk.security.springmvc.interceptor.AuthenticateInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * <p>越努力，越幸运</p>
 * 相当于springmvc.xml
 * @author appleyk
 * @version V.0.1.1
 * @blob https://blog.csdn.net/appleyk
 * @date created on  7:16 下午 2021/1/3
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.appleyk.security.springmvc"},
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
value={Controller.class, RestController.class})})
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AuthenticateInterceptor authInterceptor;

    // 配置视图解析器
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        // 设置前缀
        viewResolver.setPrefix("META-INF/view/");
        // 设置后缀
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    // 添加url对应跳转的视图（jsp）
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 根目录，直接跳转到登录页
        registry.addViewController("/").setViewName("login");
    }

    // 添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor).addPathPatterns("/auth/r/**");
    }
}
